﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace EnterpriseFund.Common
{
    public class OperateLogHelper
    {
        /// <summary>
        /// 增加功能性（页面）日志
        /// </summary>
        /// <param name="perateLogModel"></param>
        public static void AddFunctionOperateLog(string operateContent, string param, string note)
        {
            //BLL.OperateLog_MVC operateLogBLL = new BLL.OperateLog_MVC();
            //Model.OperateLog_MVC operateLogModel = new Model.OperateLog_MVC();
            //operateLogModel.FOperateContent = operateContent;
            //operateLogModel.FOperateLevel = "F";
            //operateLogModel.FParam = param;
            //operateLogModel.FNote = note;
            //SetOperationModelValue(ref operateLogModel);
            //operateLogBLL.AddOperateLog(operateLogModel);
        }
        /// <summary>
        /// 增加记录(增，删，改等操作）日志
        /// </summary>
        /// <param name="operateProp">表名+关键字段名</param>
        /// <param name="operatePropValue">关键字段值</param>
        public static void AddRecordOperateLog(string operateContent, string operateProp, string operatePropValue, string note)
        {
            //BLL.OperateLog_MVC operateLogBLL = new BLL.OperateLog_MVC();
            //Model.OperateLog_MVC operateLogModel = new Model.OperateLog_MVC();
            //operateLogModel.FOperateContent = operateContent;
            //operateLogModel.FOperateLevel = "R";
            //operateLogModel.FNote = note;
            //operateLogModel.FOperateProp = operateProp;
            //operateLogModel.FOperatePropValue = operatePropValue;
            //SetOperationModelValue(ref operateLogModel);
            //operateLogBLL.AddOperateLog(operateLogModel);
        }

        /// <summary>
        /// 生成日志类
        /// </summary>
        /// <param name="functNo">菜单ID</param>
        /// <param name="functName">菜单名称</param>
        /// <param name="visitfilename">被访问文件名称</param>
        /// <param name="param">访问文件参数</param>
        public static void setOperateLog(string functNo, string functName, string visitfilename, string param)
        {
            Model.OperateLog operateLogObject = new Model.OperateLog();
            operateLogObject.FLogID = HttpContext.Current.Session["LogID"].ToString();  //用户登陆号
            operateLogObject.Fdate = DateTime.Now;             //操作时间
            operateLogObject.FFunctNo = functNo;                   //菜单项编号 1
            operateLogObject.FfunctName = functName;           //菜单项名称 1
            operateLogObject.Fnote = "";                        //备注
            operateLogObject.Fhost = System.Net.Dns.GetHostName(); //客户端机器名
            operateLogObject.Fcip = HttpContext.Current.Request.UserHostAddress;        //客户端IP
            operateLogObject.FMethod = HttpContext.Current.Request.RequestType;         //方法
            operateLogObject.FUserAgent = HttpContext.Current.Request.UserAgent;   //用户代理
            if (HttpContext.Current.Request.UrlReferrer == null)
            {
                operateLogObject.FReferer = null;
            }
            else
            {
                operateLogObject.FReferer = HttpContext.Current.Request.UrlReferrer.ToString(); //引用站点,这条测试时注意是否正确
            }
            operateLogObject.FsessionID = HttpContext.Current.Session.SessionID;            //会话ID
            operateLogObject.fbrowser = HttpContext.Current.Request.Browser.Browser;        //浏览器
            operateLogObject.fos = HttpContext.Current.Request.Browser.Platform;            //操作系统
            operateLogObject.fvisitfilename = HttpContext.Current.Server.MapPath(visitfilename); //被访问文件 1
            operateLogObject.fparam = param;                             //参数 1
            operateLogObject.FEntityNo = HttpContext.Current.Session["EntityNo"].ToString();  //公司编号
            BLL.OperateLog operateLogBLL = new BLL.OperateLog();
            //保存类
            operateLogBLL.Add(operateLogObject);

        }

        /// <summary>
        /// 设置日志实体类的值
        /// </summary>
        /// <param name="operateLogModel"></param>
        //private static void SetOperationModelValue(ref Model.OperateLog_MVC operateLogModel)
        //{
        //    string visitAddress = HttpContext.Current.Request.Url.AbsoluteUri;
        //    operateLogModel.FVisitAddress = visitAddress.Split(new char[1] { '?' })[0];
        //    operateLogModel.FLogID = OperateSession.get(GlobalConstant.PARAM_LOGINID);
        //    operateLogModel.FLogName = OperateSession.get(GlobalConstant.PARAM_USERNAME);
        //    operateLogModel.FStartTime = DateTime.Now;
        //    operateLogModel.FOverTime = operateLogModel.FStartTime;
        //    operateLogModel.FHost = System.Net.Dns.GetHostName();

        //    string ip = HttpContext.Current.Request.UserHostAddress;
        //    if (ip == "::1")
        //    {
        //        ip = GenerateUtil.GetLocalIP();
        //    }
        //    operateLogModel.FCip = ip != string.Empty ? ip : HttpContext.Current.Request.UserHostAddress;

        //    operateLogModel.FMethod = HttpContext.Current.Request.RequestType;
        //    operateLogModel.FUserAgent = HttpContext.Current.Request.UserAgent;
        //    operateLogModel.FSessionID = HttpContext.Current.Session.SessionID;
        //    operateLogModel.FBrowser = HttpContext.Current.Request.Browser.Browser + HttpContext.Current.Request.Browser.Version;
        //    operateLogModel.FOS = HttpContext.Current.Request.Browser.Platform;
        //}
    }
}